﻿using System;
using System.Windows.Forms;
using System.IO;

namespace KiraSh.ResumeTracker
{
    // singleton class, because just one master 
    // resume is allowed for profile
    public partial class frmAddMaster : Form
    {
        // singleton instance
        private static frmAddMaster instance = null;

        private frmAddMaster()
        {
            InitializeComponent();
            this.Text = CurrentContext.Name + " | Profile: " + CurrentContext.Profile.Name;
        }

        // return an instance of the form
        public static frmAddMaster showForm()
        {
            if (instance == null)
            {
                instance = new frmAddMaster();
            }
            // show the form and bring it to front
            instance.Show();
            instance.BringToFront();

            return instance;
        }

        // save resume clicked
        private void btnAddMaster_Click(object sender, System.EventArgs e)
        {
            // the resume file has to be saved
            this.wrdResume.ExecWB(SHDocVw.OLECMDID.OLECMDID_SAVE, SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER);

            this.Close();
        }

        // add Word instance to the box
        private void frmAddMaster_Load(object sender, System.EventArgs e)
        {
            createLoadDoc(CurrentContext.Profile.Name, wrdResume);
        }

        // create file
        private void createLoadDoc(string dirName, AxSHDocVw.AxWebBrowser obj)
        {
            // make sure there is a folder for current dirName
            string dir = PathHelper.MapCombine(dirName);
            string path = CurrentContext.Profile.Path;

            // create subdirectory if it doesn't exist
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
            // create file if it doesn't exist
            if (!File.Exists(path))
            {
                FileStream f = File.Create(path);
                f.Close();
            }

            Object refmissing = System.Reflection.Missing.Value;

            /* load the file into the word doc instance
             * remove toolbars
             */
            obj.NavigateComplete2 += new AxSHDocVw.DWebBrowserEvents2_NavigateComplete2EventHandler(NavigateComplete);
            obj.Navigate(path, ref refmissing, ref refmissing, ref refmissing, ref refmissing);
        }

        // remove word toolbox
        private void NavigateComplete(object sender, AxSHDocVw.DWebBrowserEvents2_NavigateComplete2Event e)
        {
            ((AxSHDocVw.AxWebBrowser)sender).ExecWB(SHDocVw.OLECMDID.OLECMDID_HIDETOOLBARS, SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER);
        }

        // dispose word object in order to release the file and 
        // release word to avoid problems with too many word instances open
        private void frmAddMaster_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.wrdResume.Dispose();
            instance = null;
        }
    }
}
